package com.etc;

import com.etc.dao.CourseDao;
import com.etc.dao.StudentCourseDao;
import com.etc.dao.StudentDao;
import com.etc.dao.UserDao;
import com.etc.dao.impl.CourseDaoImpl;
import com.etc.dao.impl.StudentCourseDaoImpl;
import com.etc.dao.impl.StudentDaoImpl;
import com.etc.dao.impl.UserDaoImpl;
import com.etc.model.Course;
import com.etc.model.Student;
import com.etc.model.StudentCourse;
import com.etc.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;


import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class test {

    SqlSession sqlSession = null;
    SqlSessionFactory sql= null;
    UserDao userDao = null;
    StudentDao studentDao = null;
    CourseDao courseDao = null;
    StudentCourseDao studentCourseDao = null;
    public static Random ra = new Random();
    @Before
    public void init(){
        //定义读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;
        try{
            reader = Resources.getResourceAsReader(resources);
        }catch (Exception e){
            e.fillInStackTrace();
        }
        //初始化MyBatis,创建SqlSessionFactory类的实例
        sql = new SqlSessionFactoryBuilder().build(reader);
        sqlSession = sql.openSession();

    }
    @Test
    public void userByAll(){
        userDao = new UserDaoImpl(sql);
        List<User> userByAll = userDao.userByAll();
        for(User u : userByAll) {
            System.out.println(u.getId() + "," + u.getName() + "," + u.getSex());
        }
    }
    @Test
    public void userById(){
        userDao = new UserDaoImpl(sql);
        User userById = userDao.userById(2);
        System.out.println("用户ID："+ userById.getId() + "用户名字：" + userById.getName() + "用户性别：" + userById.getSex());
    }
    @Test
    public void deleteById(){
        userDao = new UserDaoImpl(sql);
        Integer deleteById = userDao.deleteById(1);
        if(deleteById == 1){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
    @Test
    public void insert(){
        userDao = new UserDaoImpl(sql);
        User user = new User();
        user.setId(1);
        user.setName("lisi");
        user.setSex("nv");
        Integer insert = userDao.insert(user);
        if(insert == 1){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    @Test
    public void StudentInsert(){
        studentDao = new StudentDaoImpl(sql);
        List<Student> studentList = new ArrayList<Student>();
        //初始化时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 20; i++) {
            //获取当前时间
            String id=format.format(new Date());
            //去掉符号
            id = id.replace("-", "").replace(" ", "").replace(":", "");
            //随机生成数id
            id = "student" + getRowKey(id);
            Student student = new Student();
            student.setSid(id);
            String name="Tom"+i;
            student.setSname(name);
            //添加到list集合
            studentList.add(student);
        }
        studentDao.StudentInsert(studentList);
    }
    public String getRowKey(String id){
        return id+""+ra.nextInt(99999);
    }
    @Test
    public void CourseInsert(){
        courseDao = new CourseDaoImpl(sql);
        List<Course> courseList = new ArrayList<Course>();
        //初始化时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 20; i++) {
            //获取当前时间
            String id = format.format(new Date());
            //去掉符号
            id = id.replace("-", "").replace(" ", "").replace(":", "");
            //随机生成数id
            id = "course" + getRowKey(id);
            Course course = new Course();
            course.setCid(id);
            String name = "Course"+i;
            course.setCname(name);
            //添加到list集合
            courseList.add(course);
        }
        courseDao.CourseInsert(courseList);
    }
    @Test
    public void StudentCourseInsert(){
        studentCourseDao = new StudentCourseDaoImpl(sql);
        List<StudentCourse> list = new ArrayList<StudentCourse>();
        String stu1 = "student2019061715300314983_course2019061715112811750";
        String stu2 = "student2019061715300314983_course2019061715112816848";
        String stu3 = "student2019061715300314983_course2019061715112819331";
        String stu4 = "student201906171530031646_course2019061715112811750";
        String stu5 = "student201906171530031646_course2019061715112816848";
        String stu6 = "student2019061715300316915_course2019061715112819331";
        //课程1 2 3   course2019061715112811750 course2019061715112816848 course2019061715112819331
        StudentCourse studentCourse1 = new StudentCourse();
        studentCourse1.setScid(stu1);
        StudentCourse studentCourse2 = new StudentCourse();
        studentCourse2.setScid(stu2);
        StudentCourse studentCourse3 = new StudentCourse();
        studentCourse3.setScid(stu3);
        StudentCourse studentCourse4 = new StudentCourse();
        studentCourse4.setScid(stu4);
        StudentCourse studentCourse5 = new StudentCourse();
        studentCourse5.setScid(stu5);
        StudentCourse studentCourse6 = new StudentCourse();
        studentCourse6.setScid(stu6);
        list.add(studentCourse1);
        list.add(studentCourse2);
        list.add(studentCourse3);
        list.add(studentCourse4);
        list.add(studentCourse5);
        list.add(studentCourse6);
        studentCourseDao.StudentCourseInsert(list);
    }
    @Test
    public void StudentCourseAll(){
        studentCourseDao = new StudentCourseDaoImpl(sql);
        List<StudentCourse> list = studentCourseDao.StudentCourseAll();
        for(StudentCourse studentCourse : list){
            System.out.println(studentCourse.getScid());
        }
    }

    @Test
    public void selectById(){
        studentCourseDao = new StudentCourseDaoImpl(sql);
        courseDao = new CourseDaoImpl(sql);
        List<StudentCourse> studentCourses = studentCourseDao.selectById("student2019061715300314983_");
        for(StudentCourse id : studentCourses){
            String scid = id.getScid();
            String  s = scid.split("_")[1];
            Course courseList = courseDao.courseById(s);
            System.out.println(courseList.toString());
        }
    }
    @Test
    public void scById(){
        studentCourseDao = new StudentCourseDaoImpl(sql);
        studentDao = new StudentDaoImpl(sql);
        List<StudentCourse> studentCourses = studentCourseDao.scById("_course2019061715112816848");
        for(StudentCourse studentCourse : studentCourses){
            String id = studentCourse.getScid();
            String s = id.split("_")[0];
            Student student = studentDao.studentById(s);
            System.out.println(student.toString());
        }
    }
    @Test
    public void deleteId(){
        studentCourseDao = new StudentCourseDaoImpl(sql);
        Integer id = studentCourseDao.deleteId("student2019061715300314983_course2019061715112816848");
        if(id == 1){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }

    }
}
